package com.example.main

import android.Manifest
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.RoutePath
import com.example.common.base.BaseActivity
import com.example.main.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import android.os.Build
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController

@AndroidEntryPoint
@Route(path = RoutePath.ROUTE_MAIN)
class MainActivity : BaseActivity<ActivityMainBinding>() {
    private lateinit var navHostFragment: NavHostFragment
    private lateinit var navController: NavController
    override fun initViews() {
        //关联底部导航组件
        navHostFragment = supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController

        mDataBinding.mainBottomNav.setupWithNavController(navController)

        //校验APP权限
        checkAppPermission()
    }

    override fun getLayoutId(): Int  = R.layout.activity_main

    private fun checkAppPermission() {
        //方案一：系统提供的权限检查
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(arrayOf(
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.FOREGROUND_SERVICE
            ), 103)
        }

        //方案: RxPermission
//        RxPermissions(this@MainActivity).request(
//            Manifest.permission.CAMERA,
//            Manifest.permission.READ_PHONE_STATE
//        )

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 103){
            //判断返回权限
            if(!permissions.contains(Manifest.permission.INTERNET)){
                showToastMessage("无网络权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                showToastMessage("无外部存储权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.CAMERA)){
                showToastMessage("无相机权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.RECORD_AUDIO)){
                showToastMessage("无麦克风权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.READ_PHONE_STATE)){
                showToastMessage("无手机状态权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.FOREGROUND_SERVICE)){
                showToastMessage("无前台服务权限，请手动开启")
            }
        }
    }


}